package com.uhfsat;

import com.jme3.app.SimpleApplication;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;
import com.jme3.util.SkyFactory;
import com.jme3.util.TangentBinormalGenerator;

/**
 * test
 *
 * @author normenhansen
 */
public class TextureMaterialsTest extends SimpleApplication {

	public static void main(String[] args) {
		TextureMaterialsTest app = new TextureMaterialsTest();
		app.start();
	}

	public void createCube(Vector3f localTranslation, String materialDefinition) {
		Box box = new Box(2.5f, 2.5f, 2.5f);
		TangentBinormalGenerator.generate(box);
		Spatial spatial = new Geometry("Box", box);
		spatial.setLocalTranslation(localTranslation);
		spatial.rotate(0f, (float) Math.PI / 3f, (float) Math.PI / 3f);
		Material material = assetManager.loadMaterial(materialDefinition);
		spatial.setMaterial(material);
		rootNode.attachChild(spatial);
	}

	@Override
	public void simpleInitApp() {



		createCube(new Vector3f(20.0f, -5.0f, 0.0f), "MatDefs/metal_plates_15.j3m");
		//createCube(new Vector3f(20.0f, -5.0f, 0.0f), "MatDefs/metal_plates_18.j3m");
		//createCube(new Vector3f(20.0f, -5.0f, 0.0f), "MatDefs/metal_plates_18.j3m");


		// Add sun
		DirectionalLight sun = new DirectionalLight();
		sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
		rootNode.addLight(sun);

		// Add ambiaent light
		AmbientLight als = new AmbientLight();
		als.setColor(ColorRGBA.DarkGray);
		rootNode.addLight(als);
	}

	public void setTextureScale(Spatial spatial, Vector2f vector) {
		if (spatial instanceof Node) {
			Node findingnode = (Node) spatial;
			for (int i = 0; i < findingnode.getQuantity(); i++) {
				Spatial child = findingnode.getChild(i);
				setTextureScale(child, vector);
			}
		} else if (spatial instanceof Geometry) {
			((Geometry) spatial).getMesh().scaleTextureCoordinates(vector);
		}
	}

	protected void setTextureOfObject(Spatial spatial) {
		Texture tex = assetManager.loadTexture("Models/white-tile-texture.jpg");
		tex.setWrap(WrapMode.Repeat);

		Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
		mat1.setTexture("ColorMap", tex);

		setTextureScale(spatial, new Vector2f(2, 2));

		spatial.setMaterial(mat1);
	}

	@Override
	public void simpleUpdate(float tpf) {
		//TODO: add update code
	}

	@Override
	public void simpleRender(RenderManager rm) {
		//TODO: add render code
	}
}
